package top.goldenyear.porpoise.common.core.util.security;

import cn.hutool.v7.core.text.CharSequenceUtil;
import cn.hutool.v7.crypto.SecureUtil;

/**
 * @author airhead
 */
public class PasswordUtils {
  public static String encrypt(String salt, String plainPassword) {
    return SecureUtil.sha256().digestHex(salt + plainPassword);
  }
  
  public static String encrypt(String plainPassword) {
    String salt = SecureUtil.md5(plainPassword);
    return SecureUtil.sha256().digestHex(salt + plainPassword);
  }

  public static boolean verify(String salt, String password, String cipherPassword) {
    try {
      return CharSequenceUtil.equals(cipherPassword, encrypt(salt, password));
    } catch (Exception e) {
      return false;
    }
  }

  public static boolean verify(String plainPassword, String cipherPassword) {
    try {
      return CharSequenceUtil.equals(cipherPassword, encrypt(plainPassword));
    } catch (Exception e) {
      return false;
    }
  }
}
